package com.hamizhong.iflowers.web.action;

import java.util.List;

import com.hamizhong.iflowers.service.IUserService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.web.base.MyBaseAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.UserVO;

public class DoLoginAction extends MyBaseAction {
	
	private IUserService iUserService;
	
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}
	
	@Override
	protected String doRequest() {
		logger.info("----------------- Hello DoLoginAction -----------------");
		String loginName = getRQ().getParameter("loginName");
		String loginPassword = getRQ().getParameter("loginPassword");
		String requestUri = (String) getRQ().getAttribute(MyConstants.IFLOWERS_LOGIN_LAST_REQUEST_URI);
		logger.info("loginName:"+loginName);
		logger.info("loginPassword:"+loginPassword);
		logger.info("requestUri:"+requestUri);
		UserVO userVO = new UserVO();
		if(loginName.contains("@")) {
			userVO.setUserEmail(loginName);
		}else {
			userVO.setUserPhone(loginName);
		}
		userVO.setUserPassword(loginPassword);
		IOPResultDto dto = iUserService.doLoginUser(userVO);
		if(dto.isSuccess()) {
			
			IObjectVO iObjectVO = (IObjectVO) dto.getFirstResult();
			UserVO userVOLogin = iObjectVO.getUserVOList().get(0);
			getSSN().setAttribute(MyConstants.IFLOWERS_LOGIN_USER_VO, userVOLogin);
			
			String validFlag = (String)getSSN().getAttribute(MyConstants.IFLOWERS_LOGIN_LAST_REQUEST_URI_VALID_FLAG);
			if(validFlag != null && validFlag.equals(MyConstants.IFLOWERS_LOGIN_LAST_REQUEST_URI_VALID_YES)) {
				getSSN().setAttribute(MyConstants.IFLOWERS_LOGIN_LAST_REQUEST_URI_VALID_FLAG, MyConstants.IFLOWERS_LOGIN_LAST_REQUEST_URI_VALID_OK);
			}
			
			return SUCCESS;
		}else {
			getRQ().setAttribute("errorMsg", dto.getErrorMSG());
			return INPUT;
		}
	}

}
